﻿using System.Web.Mvc;
using WebSite.Controllers;

namespace ControleRepresentante.UnitTests
{
    public static class TestScenarioLoginHelper
    {
        public static void LoginAsAdmin(this TestScenario testScenario)
        {
            testScenario.LoginAs(TestScenario.AdminUsername);
        }

        public static void LoginAs(this TestScenario testScenario, string username)
        {
            using (LoginController loginController = new LoginController(testScenario.RepositoryProvider, testScenario.SessionConfiguration))
            {
                loginController.Logout();
                ViewResult viewResult = loginController.Index(username, username) as ViewResult;
                if (viewResult != null)
                {
                    throw new LoginException(viewResult.ViewBag.Message);
                }
            }
        }

        public static void Logout(this TestScenario testScenario)
        {
            using (LoginController loginController = new LoginController(testScenario.RepositoryProvider, testScenario.SessionConfiguration))
            {
                loginController.Logout();
            }
        }
    }
}